//宫格
import uniGrid from "/uni_modules/uni-grid/components/uni-grid/uni-grid.vue";
//公共
import myHeader from "/pages/headerfoot/pages/header.vue";
//骨架
import uniSkeleton from "/plugin/skeleton/components/heaven-skeleton/heaven-skeleton.vue";
//公共公益方法
import commonBenefit from '@/common/commonbenefit.js';
//公共方法
import Base from '@/common/base.js';
//语言
import Lang from '@/utils/lang.js';
//微信api
import weChat from '@/lib/wechat.js';
//cache缓存
import Cache from '@/utils/cache.js';

export default {
	components: {
		myHeader,
		uniGrid
	},
	data() {
		return {
			hint: Lang.lang.hint, //提示
			confirmsubmission: Lang.lang.confirmsubmission, //确认提交吗
			clearqueue: Lang.lang.clearqueue, //清空
			mentalarithmetic: Lang.lang.mentalarithmetic, //头部标题
			completeanswer: Lang.lang.completeanswer, //完成答题
			auxiliary: Lang.lang.auxiliary, //第
			topic: Lang.lang.topic, //题',
			nextproblem: Lang.lang.nextproblem, //下一题
			answertitle: Lang.lang.answertitle, //答案
			answertitletip: Lang.lang.answertitletip, //答案
			loadingtitle: Lang.lang.loadingtitle, //记载中

			randnumberstr: '', //数字一
			answer: 0, //答案
			numberlist: [], //数字列表
			answerlist: [], //答案列表
			myanswerlist: [], //我的答案列表
			text: '', //我的输入
			subjectanswer: 0, //我的答题数

			loading: true,
			time: '9', //分钟
			second: '00', //秒
			totaltime: 450, //秒
			countdownstr: Lang.lang.countdown, //倒计时


			timer: {}, //倒计时
			answertime: 0, //答题用时

			teamclass: Cache.get('gradeid'), //年级

			model: 4, //模
			modelpage: 1, //模页数
			modeltime: 0, //模次数
			interval: 180, //时间间隔
			// interval: 10,
			occurrencenumber: 7, //出现次数
			occurrencenumberpage: 0, //每次出现次数

			teammodelnumber: 0, //班级与模次数
			score: 0, //我的分数
			getscore: 10, //获取分数
			ifdowmpage: true, //是否下一题
			notrepate: true,
		}
	},

	onLoad() {
		var that = this;

		that.time = '9'; //分钟1
		that.second = '00'; //秒
		that.totaltime = 450; //秒
		that.answertime = 0; //答题用时
		that.answer = 0;
		that.randnumberstr = ''; //数字一
		that.answer = 0; //答案
		that.numberlist = []; //数字列表
		that.answerlist = []; //答案列表
		that.myanswerlist = []; //我的答案列表
		that.text = ''; //我的输入
		that.score = 0; //我的分数
		that.subjectanswer = 0; //我的答题数1


		that.ifreglogin(); //判断是否需要注册或者登录与其他1
		that.ifsubject(); //判断答题数量

		that.getanswer(); //获取
		that.loading = false;
		that.countdown(); //倒计时



	},
	methods: {
		//判断是否需要注册或者登录与其他
		async ifreglogin() {
			var that = this;
			//注册
			await Base.globalauthorization();

			//判断学生是否注册
			await commonBenefit.ifStudenter().then(res => {
				if (res) {
					uni.redirectTo({
						url: commonBenefit.automaticurl('studentreg')
					})
				}
			});
		},
		//判断答题数量
		ifsubject() {
			var that = this;
			//答题数量加1
			that.subjectanswer++;
		},
		//3年级及以上时间分割
		threeslicedtime(teamclass) {
			var data = {};
			data['school_id'] = Cache.get('schoolid');
			data['grade_id'] = teamclass;
			return new Promise((resolve, reject) => {
				commonBenefit.getTeamGradeOptionApiMethod(data).then(res => {
						resolve(res)
				});
			})
		},
		 
		//获取答案
		async getanswer() {
			var that = this;
			//时间分割
			var threeslicedtime = await that.threeslicedtime(that.teamclass);
			//上时间
			var nowindex = 0;
			//时间匹配1
			try {
				threeslicedtime.forEach((item, index) => { //js遍历数组
					var val = parseInt(item.timeend);
					if (val >= that.answertime) {
						nowindex = index;
						throw ('终止循环'); // 抛出一个异常
					}
				});
			} catch (e) {
				console.log('catch内容：' + e); // catch内容：终止循环
			}
			var nowthreeslicedtime = threeslicedtime[nowindex];
			console.log(nowthreeslicedtime);


			var nowtopic = []; //当前题
			var nowtopicnumber = 0; //当时答案
			var nowtopicnumberstr = ''; //当前题

			var onesubjet = ''; //第一个
			var twosubjet = ''; //第二个
			//算式
			var operations = nowthreeslicedtime.operation;
			var operation = that.operation(operations);

			//第一个
			var numa = 1;
			if (nowthreeslicedtime.first == '2D') {
				var number = Base.randNumber(2, 3);
				for (var p = 1; p < number; p++) { //次方
					numa = numa * 10;
				}
			} else if (parseInt(nowthreeslicedtime.first) >= 2) {
				for (var p = 1; p < parseInt(nowthreeslicedtime.first); p++) { //次方
					numa = numa * 10;
				}
			}
			var mixa = 1 * numa;
			var maxa = 9 * numa;
			onesubjet = Base.randNumber(mixa, maxa);

			//第二个
			var num = 1;
			if (parseInt(nowthreeslicedtime.two) >= 2) {
				for (var p = 1; p < parseInt(nowthreeslicedtime.two); p++) { //次方
					num = num * 10;
				}
			}

			var mix = 1 * num;
			var max = 9 * num;
			if (operation == "-" || operation == "÷") {
				//判断位数是否相等
				if (nowthreeslicedtime.first == nowthreeslicedtime.two) {
					max = onesubjet;
				}
			}
			twosubjet = Base.randNumber(mix, max);

			nowtopicnumberstr = onesubjet + operation + twosubjet; //题
			//计算
			if (operation == "+") {
				nowtopicnumber = parseFloat(onesubjet) + parseFloat(twosubjet); //答案
			}
			if (operation == "-") {
				nowtopicnumber = parseFloat(onesubjet) - parseFloat(twosubjet); //答案
			}
			if (operation == "*") {
				nowtopicnumber = parseFloat(onesubjet) * parseFloat(twosubjet); //答案
			}
			if (operation == "÷") {
				let tempVal = parseFloat(onesubjet) / parseFloat(twosubjet); //答案
				let realVal = parseFloat(tempVal).toFixed(5);
				nowtopicnumber = realVal.substring(0, realVal.length - 1);
				nowtopicnumber = parseFloat(nowtopicnumber);
			}

			that.answerlist.push(nowtopicnumber); //推入答案
			that.numberlist.push(nowtopicnumberstr); //推入题
			that.randnumberstr = nowtopicnumberstr; //题
			that.answer = nowtopicnumber;
			that.text = ''; //重置输入
			that.ifdowmpage = true;

		},

		//点击数字
		number(number) {
			var that = this;
			that.notrepate = !that.notrepate;
			if (number == '99') { //下一题
				that.down();
			} else if (number == '999') { //不能下一题

			} else if (number == '77') { //点
				that.text = that.text + '.';
			} else if (number == '88') { //清空
				that.clearqueuemethod();
			} else {
				that.text = that.text + number;
			}
		},
		//运算
		operation(way) {
			//1=加 2=减 3=加减 4=乘 5=除 6=乘除 7=全部
			var arr = [];
			if (way == 1) {
				arr[1] = '+';
				var key = 1;
			} else if (way == 2) {
				arr[1] = '-';
				var key = 1;
			} else if (way == 3) {
				var mix = 1;
				var max = 2;
				arr[1] = '+';
				arr[2] = '-';
				var key = Base.randNumber(mix, max);
			} else if (way == 4) {
				arr[1] = '*';
				var key = 1;
			} else if (way == 5) {
				arr[1] = '÷';
				var key = 1;
			} else if (way == 6) {
				var mix = 1;
				var max = 2;
				arr[1] = '*';
				arr[2] = '÷';
				var key = Base.randNumber(mix, max);
			} else if (way == 7) {
				var mix = 1;
				var max = 4;
				arr[1] = '+';
				arr[2] = '-';
				arr[3] = '*';
				arr[4] = '÷';
				var key = Base.randNumber(mix, max);
			}
			return arr[key];
		},
		//清空
		clearqueuemethod() {
			var that = this;
			if (that.text) {
				that.text = that.text.slice(0, -1); // 去除最后一个字符
			}
		},
		//提交
		submin() {
			var that = this;
			uni.showModal({
				title: that.hint,
				content: that.confirmsubmission,
				showCancel: true,
				success: function(res) {
					if (res.confirm) {
						that.submitdata(); //数据提交
					} else if (res.cancel) {
						console.log('用户点击取消');
					}

				},
			});
		},
		//数据提交
		submitdata() {
			var that = this;
			that.ifanswer(); //判断答案相关
			//清除定时器
			clearInterval(that.timer)
			var data = {};
			data['grading_questions'] = JSON.stringify(that.numberlist); //题目
			data['questions_answer'] = JSON.stringify(that.answerlist); //正确答案
			data['my_answer'] = JSON.stringify(that.myanswerlist); //用户填写答案
			data['article_id'] = 0;
			data['project_id'] = 0;
			data['readingtime'] = 0;
			data['answertime'] = that.answertime; //答题用时
			data['allscore'] = that.score; //分数
			data['grading_type'] = 'zxys';
			commonBenefit.setUserProjectApiMethod(data).then(res => {
				if (res) {
					var id = res.id;
					var projectanswerend = commonBenefit.automaticurl('projectanswerend') + "?id=" + id;
					uni.reLaunch({
						url: projectanswerend
					})
				}
			});
		},
		//判断分数
		ifscore() {
			var that = this;
			//答案正确
			if (parseFloat(that.text) == parseFloat(that.answer)) {
				that.score = that.score + that.getscore;
			}
		},
		//下一题
		down() {
			var that = this;
			that.occurrencenumberpage++
			that.ifdowmpage = false;
			//判断答题数量
			that.ifsubject();
			that.ifanswer(); //判断答案相关
			that.getanswer(); //获取答案	
		},
		//判断答案相关
		ifanswer() {
			var that = this;
			//推入我的输入
			that.myanswerlist.push(that.text);
			that.ifscore(); //判断分数1

		},
		//倒计时
		countdown(that) {
			var that = this;
			var times = parseInt(that.time); //分钟
			var seconds = parseInt(that.second); //秒
			that.timer = setInterval(() => {
				that.answertime++; //答题用时
				if (seconds <= 0) {
					if (times > 0) {
						that.second = seconds = 59;
						that.time = --times; //减分钟
					} else {
						//时间结束
						clearInterval(that.timer)
						that.submitdata(); //数据提交
					}
				} else {
					--seconds; //减秒数
					if (seconds <= 9) {
						that.second = '0' + seconds;
					} else {
						that.second = seconds;
					}
				}
			}, 1000);
		},
	}
}